$().ready(function() {
    /*
    $('#line').val('');
    $('#enterStation').hide();
    $('#rate').val('0');
    $('#example').datetimepicker();
    $('#line').change(function(){
        var obj = document.getElementById("exitStation");
        var obj2 = document.getElementById("enterStation");
        var obj_default = 'เลือกด่าน';

        while(obj2.childNodes.length>0){
            obj2.removeChild(obj2.childNodes[0]);
        }
        obj2[0] = new Option(obj_default,'');

        if($('#line').val() == "04" ||
            $('#line').val() == "06") {
            $('#enterStation').show();
            $('#dEnterStation').text('ด่านเข้า :');
            $('#dExitStation').text('ด่านออก :');
        } else {
            $('#enterStation').hide();
            $('#dEnterStation').text('');
            $('#dExitStation').text('ด่าน :');
        }
        if($("#line").val() ==""){
            while(obj.childNodes.length>0){
                obj.removeChild(obj.childNodes[0]);
            }
            obj[0] = new Option(obj_default,'');
        } else {
            var url="CallStations";
            var dataSet={
                line: $("#line").val(),
                type: "all"
            };
            $.post(url,dataSet,function(data){
                while(obj.childNodes.length>0){
                    obj.removeChild(obj.childNodes[0]);
                }
                arr_list=data.split("/");
                if(arr_list.length-1!=0){
                    for (var i=0; i < eval(arr_list.length-1); i++) {
                        shop_arr=arr_list[i].split(",");
                        obj.options[i] = new Option(shop_arr[1],shop_arr[0]);
                    }
                }else{
                    while(obj.childNodes.length>0){
                        obj.removeChild(obj.childNodes[0]);
                    }
                    obj[0] = new Option(obj_default,'');
                }
            });
        }

        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);

    });

    $('#exitStation').change(function(){
        var obj = document.getElementById("enterStation");
        var obj_default = 'เลือกด่าน';

        if($("#exitStation").val() ==""){
            while(obj.childNodes.length>0){
                obj.removeChild(obj.childNodes[0]);
            }
            obj[0] = new Option(obj_default,'');
        } else {
            var url="CallStations";
            var dataSet={
                line : $("#line").val() ,
                station : $("#exitStation").val(),
                type: "enter"
            };
            $.post(url,dataSet,function(data){
                while(obj.childNodes.length>0){
                    obj.removeChild(obj.childNodes[0]);
                }
                arr_list=data.split("/");
                if(arr_list.length-1!=0){
                    for (var i=0; i < eval(arr_list.length-1); i++) {
                        shop_arr=arr_list[i].split(",");
                        obj.options[i] = new Option(shop_arr[1],shop_arr[0]);
                    }
                }else{
                    while(obj.childNodes.length>0){
                        obj.removeChild(obj.childNodes[0]);
                    }
                    obj[0] = new Option(obj_default,'');
                }
            });
        }
    });

    $('#hour').keyup(function(){
        var i = $('#hour').val();
        if(i.length == 2) {
            $('#minute').focus()
        }
    });

    $('#carType').change(function(){
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#exitStation').change(function(){
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#enterStation').change(function(){
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#save').click(function(){
        submit();
    });

    $("#frm_uap001edit").validate({

        rules: {
            line: "required",
            exitStation: "required",
            enterStation: "required",
            uapDate: "required",
            hour: {
                required: true
            },
            minute: {
                required: true
            },
            carType: "required",
            color: "required",
            brand: "required",
            plate: "required",
            province: "required",
            pay: {
                required: true
            },
            emp3: "required"

        },
        messages: {
            line: " กรุณาเลือกสายทาง",
            exitStation: " กรุณาเลือกด่าน",
            enterStation: " กรุณาเลือกด่าน",
            uapDate: " กรุณาเลือกวันที่",
            hour: " กรุณากรอกชั่วโมง",
            minute: " กรุณากรอกนาที",
            carType: " กรุณาเลือกประเภทรถ",
            color: " กรุณาเลือกสี",
            brand: " กรุณาเลือกยี่ห้อ",
            plate: " กรุณากรอกเลขทะเบียนรถ",
            province: " กรุณาเลือกจังหวัด",
            pay:{
                required: " กรุณากรอกจำนวนเงินที่ชำระ"
            },
            emp3: " กรุณากรอกหัวหน้าพนักงาน"
        },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "hour"
                || element.attr("name") == "minute" )
                error.insertAfter("#minute");
            else
                error.insertAfter(element);
        },
        debug:false
    });*/


    $('#line').val('');
    $('#enterStation').hide();
    $('#card').hide();
    $('#ckCard').hide();
    $('#rate').val('0');
    $('#card').val('0');
    $('#result').val('0');
    $('#pay').val('0');
    $('#example').datetimepicker();
    $('#ckCard').attr('checked', false);



    $('#line').change(function(){
        var obj = document.getElementById("exitStation");
        var obj2 = document.getElementById("enterStation");
        var obj_default = 'เลือกด่าน';

        while(obj2.childNodes.length>0){
            obj2.removeChild(obj2.childNodes[0]);
        }
        obj2[0] = new Option(obj_default,'');


        $('#ckCard').attr('checked', false);
        $('#pay').val('0');
        $('#card').val('0');

        if($('#line').val() == "04" ||
            $('#line').val() == "06") {
            $('#enterStation').show();
            $('#dEnterStation').text('ด่านเข้า :');
            $('#dExitStation').text('ด่านออก :');
            $('#card').show();
            $('#ckCard').show();
            $('#dCard').text('ค่าปรับ IC CARD :');
        } else {
            $('#enterStation').hide();
            $('#dEnterStation').text('');
            $('#dExitStation').text('ด่าน :');
            $('#card').hide();
            $('#ckCard').hide();
            $('#dCard').text('');
        }
        if($("#line").val() ==""){
            while(obj.childNodes.length>0){
                obj.removeChild(obj.childNodes[0]);
            }
            obj[0] = new Option(obj_default,'');
        } else {
            var url="CallStations";
            var dataSet={
                line: $("#line").val(),
                type: "all"
            };
            $.post(url,dataSet,function(data){
                while(obj.childNodes.length>0){
                    obj.removeChild(obj.childNodes[0]);
                }
                arr_list=data.split("/");
                if(arr_list.length-1!=0){
                    for (var i=0; i < eval(arr_list.length-1); i++) {
                        shop_arr=arr_list[i].split(",");
                        obj.options[i] = new Option(shop_arr[1],shop_arr[0]);
                    }
                }else{
                    while(obj.childNodes.length>0){
                        obj.removeChild(obj.childNodes[0]);
                    }
                    obj[0] = new Option(obj_default,'');
                }
            });
        }

        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);

    });

    $('#pay').keyup(function() {
        var pay = $('#pay').val(),
        rate = $('#rate').val(),
        card = $('#card').val(),
        result = (parseInt(rate, 10) + parseInt(card, 10)) - parseInt(pay, 10);


        if(result < 1) {
            alert('จำนวนเงินที่ชำระไม่ถูกต้อง');
            $('#pay').val('0');
            var ex = $('#exitStation').val();
            var en = $('#enterStation').val();
            var type = $('#carType').val();
            checkRate(en,ex,type);
        } else {
            $('#result').val(result);
        }
    });

    $('#ckCard').bind('change', function () {

        if ($(this).is(':checked'))
            $("#card").val('140');
        else
            $("#card").val('');

        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#exitStation').change(function(){
        var obj = document.getElementById("enterStation");
        var obj_default = 'เลือกด่าน';

        if($("#exitStation").val() ==""){
            while(obj.childNodes.length>0){
                obj.removeChild(obj.childNodes[0]);
            }
            obj[0] = new Option(obj_default,'');
        } else {
            var url="CallStations";
            var dataSet={
                line : $("#line").val() ,
                station : $("#exitStation").val(),
                type: "enter"
            };
            $.post(url,dataSet,function(data){
                while(obj.childNodes.length>0){
                    obj.removeChild(obj.childNodes[0]);
                }
                arr_list=data.split("/");
                if(arr_list.length-1!=0){
                    for (var i=0; i < eval(arr_list.length-1); i++) {
                        shop_arr=arr_list[i].split(",");
                        obj.options[i] = new Option(shop_arr[1],shop_arr[0]);
                    }
                }else{
                    while(obj.childNodes.length>0){
                        obj.removeChild(obj.childNodes[0]);
                    }
                    obj[0] = new Option(obj_default,'');
                }
            });
        }
    });

    $('#hour').keyup(function(){
        var i = $('#hour').val();
        if(i.length == 2) {
            $('#minute').focus()
        }
    });

    $('#carType').change(function(){
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#exitStation').change(function(){
        $('#pay').val('0');
        $('#card').val('0');
        $('#ckCard').attr('checked', false);
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#enterStation').change(function(){
        $('#pay').val('0');
        $('#card').val('0');
        $('#ckCard').attr('checked', false);
        var ex = $('#exitStation').val();
        var en = $('#enterStation').val();
        var type = $('#carType').val();
        checkRate(en,ex,type);
    });

    $('#save').click(function(){
        submit();
    });

    $("#frm_uap001").validate({

        rules: {
            line: "required",
            exitStation: "required",
            enterStation: "required",
            uapDate: "required",
            hour: {
                required: true
            },
            minute: {
                required: true
            },
            carType: "required",
            color: "required",
            brand: "required",
            plate: "required",
            province: "required",
            pay: {
                required: true
            },
            emp3: "required"

        },
        messages: {
            line: " กรุณาเลือกสายทาง",
            exitStation: " กรุณาเลือกด่าน",
            enterStation: " กรุณาเลือกด่าน",
            uapDate: " กรุณาเลือกวันที่",
            hour: " กรุณากรอกชั่วโมง",
            minute: " กรุณากรอกนาที",
            carType: " กรุณาเลือกประเภทรถ",
            color: " กรุณาเลือกสี",
            brand: " กรุณาเลือกยี่ห้อ",
            plate: " กรุณากรอกเลขทะเบียนรถ",
            province: " กรุณาเลือกจังหวัด",
            pay:{
                required: " กรุณากรอกจำนวนเงินที่ชำระ"
            },
            emp3: " กรุณากรอกหัวหน้าพนักงาน"
        },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "hour"
                || element.attr("name") == "minute" )
                error.insertAfter("#minute");
            else
                error.insertAfter(element);
        },
        debug:false
    });


}); 

    

function submit() {
    $('#frm_uap001edit').submit();
}

/*
function checkRate(enter,exit,type) {
    var url="CallRate";
    var rate = "0";
    var dataSet={
        enter: enter,
        exit: exit,
        type: type
    };
    $.post(url,dataSet,function(data){
        $('#rate').val(data);
    });
}
*/

function checkRate(enter,exit,type) {
    var url="CallRate";
    var rate = "0";
    var dataSet={
        enter: enter,
        exit: exit,
        type: type
    };
    $.post(url,dataSet,function(data){
        var card = $('#card').val(),result;

        if(card == 0) {
            card = "0";
        }

        result = parseInt(card, 10) + parseInt(data, 10);

        $('#result').val(result);
        $('#rate').val(data);
    });
}

function emp1(autoObj,showObj){
    var mkAutoObj=autoObj;
    var mkSerValObj=showObj;
    new Autocomplete(mkAutoObj, function() {
        this.setValue = function(id) {
            $(mkSerValObj).val(id);
        }
        if ( this.isModified )
            this.setValue("");
        if ( this.value.length < 1 && this.isNotClick )
            return ;
        return "CallEmployee?id=" + this.value;
    });
}

function emp2(autoObj,showObj){
    var mkAutoObj=autoObj;
    var mkSerValObj=showObj;
    new Autocomplete(mkAutoObj, function() {
        this.setValue = function(id) {
            $(mkSerValObj).val(id);
        }
        if ( this.isModified )
            this.setValue("");
        if ( this.value.length < 1 && this.isNotClick )
            return ;
        return "CallEmployee?id=" + this.value;
    });
}

function emp3(autoObj,showObj){
    var mkAutoObj=autoObj;
    var mkSerValObj=showObj;
    new Autocomplete(mkAutoObj, function() {
        this.setValue = function(id) {
            $(mkSerValObj).val(id);
        }
        if ( this.isModified )
            this.setValue("");
        if ( this.value.length < 1 && this.isNotClick )
            return ;
        return "CallEmployee?id=" + this.value;
    });
}